cur_area = {
	id : 2,
	name : '321区',
	x : 0,
	y : 0
};
var CURRENT_BASIC_ROUTE_LOCATIONS = null;
var map_image = new Image();
map_image.src = "css/images/map2.png";
$(document).ready(function() {
	CanvasTool = new CanvasUtils();
	var info = $("#traffic_info");
	var mark = $("#mark");
	min_left = parseInt(info.css("left").split("p")[0]);
	min_top = parseInt(info.css("top").split("p")[0]);
	max_left = info.width() + min_left - mark.width();
	max_top = info.height() + min_top - mark.height();
	mark.easydrag();
	mark.ondrag(function(e) {
		// 限制拖拽范围
		var cur = mark;
		var top = cur.offset().top;
		var left = cur.offset().left;
		var relocate = false;
		if (top < min_top) {
			cur.css("top", min_top);
			relocate = true;
		}
		if (left < min_left) {
			cur.css("left", min_left);
			relocate = true;
		}
		if (top > max_top) {
			cur.css("top", max_top);
			relocate = true;
		}
		if (left > max_left) {
			cur.css("left", max_left);
			relocate = true;
		}
		if (!relocate) {
			var ref_left = left - min_left;
			var ref_top = top - min_top;
			var map = $("#entry_controller");
			if (map.offset()) {
				map.find("img").css({
					top : -(ref_top * 10),
					left : -(ref_left * 10)
				});
			} else {
				map = $("#canvas");
			}
			cur.html((map.offset().left + ref_left * 10) + "," + (map.offset().top + ref_top * 10));
			LEFT = map.offset().left + ref_left * 10;
			TOP = map.offset().top + ref_top * 10;
			/*if (!$("#entry_controller").offset()) {
				excursion("canvas", map_image, LEFT, TOP);
				CanvasTool.drawCircle(entry_ctrl.locations);
				if (CURRENT_BASIC_ROUTE_LOCATIONS) {
					CanvasTool = new CanvasUtils({
						points : CURRENT_BASIC_ROUTE_LOCATIONS
					});
					CanvasTool.drawLine();
				}
			}*/
			if (entry_ctrl) {
				// 判断locations是否在当前绘图区域
				$(".key_point").each(function(i) {
					var cur = $(this);
					var x = parseInt(cur.attr("x"));
					var y = parseInt(cur.attr("y"));
					cur.css({
						position : "absolute",
						'top' : (y - TOP + controller.offset().top - Math.floor(cur.width() / 2)),
						'left' : (x - LEFT + controller.offset().left - Math.floor(cur.height() / 2)),
						'z-index' : 3
					});
				});
			}
		}
		relocate = false;
	});
	mark.ondrop(function() {
		// 调整大图
		var cur = mark;
		var top = cur.offset().top;
		var left = cur.offset().left;
		var ref_left = left - min_left;
		var ref_top = top - min_top;
		var map = $("#entry_controller");
		if (map.offset()) {
			map.find("img").css({
				top : -(ref_top * 10),
				left : -(ref_left * 10)
			});
		} else {
			map = $("#canvas");
		}
		cur.html((map.offset().left + ref_left * 10) + "," + (map.offset().top + ref_top * 10));
		LEFT = map.offset().left + ref_left * 10;
		TOP = map.offset().top + ref_top * 10;
		if (!$("#entry_controller").offset()) {
			excursion("canvas", map_image, LEFT, TOP);
			CanvasTool.drawCircle(entry_ctrl.locations);
			if (CURRENT_BASIC_ROUTE_LOCATIONS) {
				CanvasTool = new CanvasUtils({
					points : CURRENT_BASIC_ROUTE_LOCATIONS
				});
				CanvasTool.drawLine();
			}
		}
		if (entry_ctrl) {
			// 判断locations是否在当前绘图区域
			$(".key_point").each(function(i) {
				var cur = $(this);
				var x = parseInt(cur.attr("x"));
				var y = parseInt(cur.attr("y"));
				cur.css({
					position : "absolute",
					'top' : (y - TOP + controller.offset().top - Math.floor(cur.width() / 2)),
					'left' : (x - LEFT + controller.offset().left - Math.floor(cur.height() / 2)),
					'z-index' : 3
				});
			});
		}
	});
});

var cur_tool = getUrlParam("tool");
switch (cur_tool) {
case 'mapEditor':
	// 当窗口加载实例化实体控制器
	window.onload = function() {
		$(".entry_controller").remove();
		$("#toolbar").html("");
		$("#toolbar").append(
				"<p class='title'>工具栏</p>" + "<p>" + "	<input type='radio' name='tools' value='construct_key_point'" + "		id='construct_key_point' checked='checked' /><label"
						+ "		for='construct_key_point'>构建道路关键点</label>" + "</p>" + "<p>" + "	<input type='radio' name='tools' value='construct_road'"
						+ "		id='construct_road' /><label for='construct_road'>构建道路</label>" + "</p>" + "<p>" + "	<input type='radio' name='tools' value='construct_other_entry'"
						+ "		id='construct_other_entry' /><label for='construct_other_entry'>构建其他实体</label>" + "</p>" + "<p class='title' id='option_title'>更多选项</p>"
						+ "<div id='tool_options' class='options'></div>" + "<p>" + "	<input type='button' value='提交所有道路信息' id='submit_all' />" + "</p>");
		entry_ctrl = new Entry_Controller('#entry_controller', {
			hello : 'helloKitty'
		});
		// 初始化路径（数据库已存在路径）
		entry_ctrl.query_exist_roads(HOST + "query_roads_by_area", 'area=' + cur_area.id);

		// 添加事件
		controller.click(function(e) {
			if (deleting) {
				deleting = false;
				return;
			}
			var loc = new Location(e);
			loc.x += LEFT;
			loc.y += TOP;
			entry_ctrl.locations[getProperId('kp_', 0)] = loc;
			entry_ctrl.addEntry(currentOperation, loc, controller, entry_ctrl.locations.length - 1);
		});

		// 选择工具
		$("p [name='tools']").click(function() {
			entry_ctrl.detect_toolbar_operation($(this));
		});

		entry_ctrl.detect_toolbar_operation($(":radio:checked"));

		$("#submit_all").click(function() {
			switch (currentOperation) {
			case entry_ctrl.ENTRY.LIGHTS:
				if (Lights != null) {
					var data = new Array();
					Lights.foreach(function(i, d, t) {
						t[i] = d.toJSON();
					}, data);
					ajax_post(HOST + "upload_lights", 'lights=' + JSON.stringify(data), function(msg) {
						if (!msg) {
							alert("OK");
						}
					});
				}
				break;
			case entry_ctrl.ENTRY.ROAD:
				if (roads.cur == -1 || roads.size() == 0)
					return;
				var roads_json = {};
				roads_json = new Array(roads.size());
				for ( var i = 0; i < roads.size(); i++) {
					if (roads.get(i).locs.size() == 0)
						return;
					roads_json[i] = roads.get(i).toJSON();
				}
				ajax_post(HOST + "upload_points_info", 'roads=' + JSON.stringify(roads_json), function(msg) {
					if (!msg) {
						var json = eval("(" + msg + ")");
						entry_ctrl.update_current_road_by_json(json);
					}
				});
				break;
			}
		});
	};
	window.onbeforeunload = function() {
		return "您正在关闭窗口,请确认数据已保存";
	};
	break;
case 'static_block':
	break;
case 'dynamic_block':
	break;
default:
	var finding = false;
	// 当窗口加载实例化实体控制器
	window.onload = function() {
		$("title").html("基本选路");
		$("#entry_controller").remove();
		$("#toolbar").html("");
		$("#toolbar").append("<p class='title'>基本寻路:</p>" + "<p id='start'>请地图上单击选择起点与终点</p>" + "<p id='end'></p>" + "<p><input type='button' id='find' value='开始基本寻路'/></p>" + "<p id='loading'></p>");
		entry_ctrl = new Entry_Controller('.entry_controller', {
			hello : 'helloKitty'
		});
		currentOperation = entry_ctrl.OPTERATION.BASIC;
		var locs;
		// 添加事件
		controller.click(function(e) {
			if (deleting) {
				deleting = false;
				return;
			}
			if (finding) {
				return;
			}
			var loc = new Location(e);
			loc.x = loc.x + LEFT;
			loc.y = loc.y + TOP;
			if (entry_ctrl.locations.length == 2) {
				entry_ctrl.locations[0] = entry_ctrl.locations[1];
				entry_ctrl.locations[1] = loc;
			} else {
				entry_ctrl.locations[entry_ctrl.locations.length] = loc;
			}
			excursion("canvas", map_image, LEFT, TOP);
			CanvasTool.drawCircle(entry_ctrl.locations);
			if (entry_ctrl.locations[0]) {
				$("#start").html("起点:(" + entry_ctrl.locations[0].x + "," + entry_ctrl.locations[0].y + ")");
				$("#start").css("color", "blue");
			} else {
				$("#start").html("请地图上单击选择起点");
			}
			if (entry_ctrl.locations[1]) {
				$("#end").html("终点:(" + entry_ctrl.locations[1].x + "," + entry_ctrl.locations[1].y + ")");
				$("#end").css("color", "red");
			} else {
				$("#end").html("请地图上单击选择终点");
			}
		});
		$("#find").click(function() {
			excursion("canvas", map_image, LEFT, TOP);
			CanvasTool.drawCircle(entry_ctrl.locations);
			locs = {};
			if (entry_ctrl.locations.length != 2) {
				alert(entry_ctrl.locations.length == 0 ? "请在地图上选择起点与终点" : "请在地图上选择终点");
				return;
			}
			finding = true;
			if(typeof(isloading)!="undefined" && isloading){
				return;
			}
			isloading=true;
			$("#loading").html("<img src='css/images/loading.gif'/>正在寻路...");
			var x = entry_ctrl.locations[0].x;
			var y = entry_ctrl.locations[0].y;
			var x1 = entry_ctrl.locations[1].x;
			var y1 = entry_ctrl.locations[1].y;
			var json = {};
			var points = [ {
				id : 0,
				x : x,
				y : y,
				area : cur_area
			}, {
				id : 0,
				x : x1,
				y : y1,
				area : cur_area
			} ];
			json = points;
			entry_ctrl.post_ajax_then_do(HOST + "basic", "points=" + JSON.stringify(json), function(json) {
				excursion("canvas", map_image, LEFT, TOP);
				json = eval("(" + json + ")");
				$("#loading").html("");
				finding = false;
				var size = json.length;
				locs = new ArrayList();
				for ( var i = 0; i < size; i++) {
					var cur = json[i];
					var loc = new Location(cur.x, cur.y, cur.id);
					locs.add(loc);
					entry_ctrl.addEntry(currentOperation, loc, controller, 0);
				}
				locs.addElementAt(0, new Location(x, y));
				locs.addElementAt(locs.size(), new Location(x1, y1));
				CURRENT_BASIC_ROUTE_LOCATIONS = locs.data();
				CanvasTool = new CanvasUtils({
					points : CURRENT_BASIC_ROUTE_LOCATIONS
				});
				CanvasTool.drawLine();
				isloading=false;
			});
		});
	};
	break;
}
